<?php

/**
 * @file barsem.php
 * @brief Clase para generar un semaforo por barras de progreso
 * @author DyrTechnologies
 * @version V 1.0
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase barsem
 */


/**
 * @brief Define la ruta a la hoja de estilos para el semaforo por barras de progreso
 *
 */

require_once(CEF_SEMAPHORE);

define("BARSEM_IMG_SRC",FULL_URL."barsem/img/progress.png");
define("RESERV_IMG_SRC",FULL_URL."barsem/img/reserve.png");
define("BARSEM_CSS",FULL_URL."barsem/barsem.css");
define("BARSEM_HEIGHT","15px");
define("MAX_WIDTH","100px");

$barsem_instance = 0;

class barsem extends semaphore {

	/**
	 * Constructor de la clase semaforo por barras de progreso
	 *
	 * @param integer $low
	 * @param integer $mid
	 * @param integer $high
	 * @param string $_name
	 * @return semaphore
	 */

	public function barsem($_name='__barsem'){

		/**
		 * @brief $barsem_instance es una variable para incrementar el numero de semaforo por barras de progresos existentes
		 */
		global $barsem_instance;
		$barsem_instance++;
		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */

		if($_name=="__barsem")
			$_name="barsem".$barsem_instance;

		$this->set_name($_name);
	}

	/**
	 * @brief Se reescriben las operaciones para calcular el valor actual
	 */

	function calc_single_barsem($a, $b, $delim='/'){
		return $this->calc_single_semaphore($a,$b,$delim='/');
	}

	function calc_dual_barsem($prev_event, $next_event, $current_quant, $desired_quant, $delim='/'){
		return $this->calc_dual_semaphore($prev_event, $next_event, $current_quant, $desired_quant, $delim='/');
	}
	/**
	 * @brief Devuelve el semaforo por barras de progreso como una cadena con sus parametros
	 *
	 * @return string $retval
	 */
	public function get_barsem(){
		return $this->get_semaphore();
	}

	private function smarty_barsem($barsem_image,$reserv_image){

		global $smarty;

		/**
		 * @brief Asignacion atributo por atributo a lo deseado en las variables de smarty
		 */
		$smarty->assign("barsem_image",$barsem_image);
		if(isset($reserv_image) && strlen($reserv_image) > 0){
		  $smarty->assign("reserv_image",$reserv_image);
		}

		/**
		 * @brief Asigna la hoja de estilo que contiene los rasgos del semaforo a una variable
		 */
		$smarty->assign("barsemcss",BARSEM_CSS);
	}
	/**
	 * @brief Asigna el semaforo por barras de progreso a la variable $smarty para su renderizacion
	 *
	 */
	public function display_barsem($fetch = false){
		$barsem_image = new imagece("barsemimg",BARSEM_IMG_SRC);
				
		$width = (100-$this->get_curr())*MAX_WIDTH/100;
		if($width > 100)
			$width = 100;
		$barsem_image->set_extra("width=\"$width.\" height=\"".BARSEM_HEIGHT."\"");
		$barsem_image->set_alt($this->get_curr()."%");
		$barsem_image = $barsem_image->display_image(true);

		if(strlen($this->get_curr_reserv()) > 0){			
		  $reserv_image = new imagece("reservimg",RESERV_IMG_SRC);
		  $width = (100-$this->get_curr_reserv())*MAX_WIDTH/100;
		  $reserv_image->set_extra("width=\"$width\" height=\"".(BARSEM_HEIGHT-5)."\"");
		  $reserv_image->set_alt($this->get_curr_reserv()."%");
		  $reserv_image = $reserv_image->display_image(true);
		}		

		if(isset($reserv_image))
		  $this->smarty_barsem($barsem_image,$reserv_image);
		else
		  $this->smarty_barsem($barsem_image,null);
		global $smarty;
   		$smarty->template_dir = dirname(__FILE__).'/';
		if(!$fetch){
			$smarty->assign( $this->get_name() , $smarty->fetch('barsem.tpl') );
			return 1;
		}
		else
			return $smarty->fetch('barsem.tpl');
	}
}

?>